<?php
/**
 * @file region.php $
 * @author LinLin (linlin@haibao.com) $
 * @date 2015-11-17 下午6:07:48 $
 * @brief  $APP文章地域
 */
namespace haibao\app\data;

class Region extends \haibao\app\data\BaseMysql{

	public function __construct(){
		parent::__construct('\haibao\app\model\data\Region');
	}
	
	/**
	 * 新增地域
	 */
	public function addRegion($model){
		$this->add($model);
		$this->setArticleRegionCache();
		$this->setRegionCache();
	}
	
	/**
	 * 修改地域排序
	 */
	public function updateRegionSort($regionId, $arrSet){
		$this->updateById($regionId, $arrSet);
		$this->setRegionCache();
		$this->setArticleRegionCache();
	}
	
	/**
	 * 修改地域状态(前台是否显示)
	 */
	public function updateRegionStatus($id, $status = \haibao\app\model\data\Region::STATUS_ENABLE){
		$this->updateById($id, array(\haibao\app\model\filter\Region::CONDITION_FIELD_NAME_STATUS=>$status));
		$this->setRegionCache();
		$this->setArticleRegionCache();
	}
	
	/**
	 * 获取地域最大sort
	 */
	public function getRegionMaxSort(){
		$sql = "select max(sort) as maxSort from Region where `Status` = %s";
		return  $this->query($sql, array(\haibao\app\model\data\Region::STATUS_ENABLE))->fetch_assoc();
	}
	
	/**
	 * 根据地域名称获取数据
	 */
	public function getRegionByName($regionName){
		return $this->query('select Id,RegionName from Region where RegionName=%s and Status<>%s limit 1', array(
				$regionName, \haibao\app\model\data\Region::STATUS_DELETE
		))->fetch_assoc();
	}
	
	/**
	 * 获取所属地域(文章列表)
	 */
	public function getArticleRegion(){
		$cache = new \haibao\app\cache\Column();
		$regionList = $cache->getCacheByKey(\haibao\app\model\data\Region::CACHE_REGION_AREA_KEY);
		if(empty($regionList)){
			$regionList = $this->setArticleRegionCache();
		}
		return $regionList;
	}
	
	
	public function setArticleRegionCache(){
		$regionList = array();
		$list = array();
		$result = $this->query('select * from Region where Status <> %s',array(\haibao\app\model\data\Region::STATUS_DELETE));
		while($result->fetch_assoc()){
			$list['Id'] = $result->getData('Id');
			$list['RegionName'] = $result->getData('RegionName');
			$list['Status'] = $result->getData('Status');
			array_push($regionList, $list);
		}
		$cache = new \haibao\app\cache\Column();
		$cache->setCache(\haibao\app\model\data\Region::CACHE_REGION_AREA_KEY, $regionList);
		return $regionList;
	}
	
	/**
	 * 获取前台显示所属地域(API接口)
	 */
	public function getRegion(){
		$cache = new \haibao\app\cache\Column();
		$regionList = $cache->getCacheByKey(\haibao\app\model\data\Region::CACHE_API_REGION_KEY);
		if(empty($regionList)){
			$regionList = $this->setRegionCache();
		}
		return $regionList;
	}
	
	public function setRegionCache(){
		$regionList = array();
		$list = array();
		$result = $this->query('select Id,RegionName from Region where Status = %s order by Sort Asc ',array(\haibao\app\model\data\Region::STATUS_ENABLE));
		while($result->fetch_assoc()){
			$list['Id'] = $result->getData('Id');
			$list['Name'] = $result->getData('RegionName');
			array_push($regionList, $list);
		}
		$cache = new \haibao\app\cache\Column();
		$cache->setCache(\haibao\app\model\data\Region::CACHE_API_REGION_KEY, $regionList);
		return $regionList;
	}
	
}